package com.longcai.cm.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.longcai.cm.common.utils.StringUtil;
import com.longcai.cm.common.utils.StringUtils;
import com.longcai.cm.dao.JaCouponMapper;
import com.longcai.cm.dao.JaGoodsInfoMapper;
import com.longcai.cm.dao.JaMemberCouponsMapper;
import com.longcai.cm.domain.JaCoupon;
import com.longcai.cm.domain.JaGoodsInfo;
import com.longcai.cm.domain.JaMemberCoupons;
import com.longcai.cm.exception.SysRuntimeException;
import com.longcai.cm.service.JaMemberCouponsService;
import com.longcai.cm.base.service.BaseServiceImpl;
import org.springframework.stereotype.Service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


/**
 * @author LBX
 */
@Service
public class JaMemberCouponsServiceImpl extends BaseServiceImpl<JaMemberCoupons> implements JaMemberCouponsService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private JaCouponMapper couponMapper;

    @Resource
    private JaMemberCouponsMapper memberCouponsMapper;

    @Resource
    private JaGoodsInfoMapper goodsInfoMapper;

    /**
     * 更新优惠券使用状态
     *
     * @param goodId   商品id
     * @param couponId 优惠券id
     * @return 优惠金额
     */
    @Override
    public BigDecimal changeUseStatusToUse(String goodId, String couponId) {
//        状态(0-未使用 1-已使用 2-已失效)
        final JaMemberCoupons coupons = updateStatus(couponId, 1);
        logger.info("优惠券状态变更-已使用");
        final JaCoupon coupon = couponMapper.selectById(coupons.getCouponsId());
        if (StringUtils.isNotBlank(coupon.getServiceInfo())) {
            final JaGoodsInfo goodsInfo = goodsInfoMapper.selectById(goodId);
            if (!goodsInfo.getServiceInfo().equals(coupon.getServiceInfo())) {
                throw new SysRuntimeException("优惠券服务类型不匹配");
            }
        }
        switch (coupon.getUseType()) {
            case 1:
            case 2:
                return coupon.getDiscountAmount();
            case 3:
                return BigDecimal.ZERO;
            default:
                throw new SysRuntimeException("优惠券类型错误");
        }
    }

    /**
     * 更新优惠券使用状态
     *
     * @param couponId 优惠券id
     */
    @Override
    public void changeUseStatusToCancel(String couponId) {
//        状态(0-未使用 1-已使用 2-已失效)
        updateStatus(couponId, 0);
        logger.info("优惠券状态变更-已退还");
    }

    /**
     * 更新优惠券使用状态
     *
     * @param couponId 优惠券id
     */
    @Override
    public void changeUseStatusToExpire(String couponId) {
//        状态(0-未使用 1-已使用 2-已失效)
        updateStatus(couponId, 2);
        logger.info("优惠券状态变更-已失效");
    }

    private JaMemberCoupons updateStatus(String couponId, Integer status) {
        final JaMemberCoupons coupons = getById(couponId);
        coupons.setStatus(status);
        updateById(coupons);
        return coupons;
    }

    /**
     * @param couponId    优惠券
     * @param couponCount 数量
     * @param memberId    用户
     */
    @Override
    public void addCoupons(String couponId, Integer couponCount, Integer memberId) {
        final JaCoupon coupon = couponMapper.selectById(couponId);
        List<JaMemberCoupons> list = new ArrayList<>(couponCount);
        for (int i = 0; i < couponCount; i++) {
            JaMemberCoupons coupons = new JaMemberCoupons();
            coupons.setId(StringUtil.getUuid());
            coupons.setCouponsId(couponId);
            coupons.setMemberId(memberId);
            coupons.setStatus(0);
            coupons.setExpireTime(DateUtil.date().offset(DateField.DAY_OF_YEAR, coupon.getEffectiveDays()).toString());
            coupons.setCreateTime(DateUtil.now());
            list.add(coupons);
        }
        memberCouponsMapper.saveList(list);
    }
}
